import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  CategoryItem,
  CategoryInfoType,
  CategorySubInfoType
} from '@/types/data'
const initCategory = [
  '居家',
  '美食',
  '服饰',
  '母婴',
  '个护',
  '严选',
  '数码',
  '运动',
  '杂项'
]
const newInitCategoryList = initCategory.map((item, index) => {
  return {
    id: index + '',
    name: item
  }
})
export default defineStore('category', {
  state () {
    return {
      username: 'zs',
      categoryList: newInitCategoryList as CategoryItem[],
      categoryInfo: {} as CategoryInfoType, //  一级分类
      categorySubInfo: {} as CategorySubInfoType
    }
  },
  actions: {
    // 获取分类数据
    async homeCategoryHead () {
      const res = await request.request<ApiRes<CategoryItem[]>>({
        url: '/home/category/head'
      })
      this.categoryList = res.data.result
      console.log('列表数据', res)
    },
    // 获取一级类目数据
    async getCategory (id: string) {
      const res = await request.request<ApiRes<CategoryInfoType>>({
        url: '/category',
        params: {
          id
        }
      })
      this.categoryInfo = res.data.result
    },
    // 获取二级类目数据
    async categorySubFilter (id: string) {
      const res = await request.request<ApiRes<CategorySubInfoType>>({
        url: '/category/sub/filter?id=' + id
      })
      this.categorySubInfo = res.data.result
      console.log('二级分分类数据', res)
    }
  }
})
